﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.LMS.Core;
using ZDSoft.LMS.Service;
using ZDSoft.LMS.Domain;
using ZDSoft.LMS.Web.Apps;
using NHibernate.Criterion;

namespace ZDSoft.LMS.Web.Controllers
{
    /// <summary>
    /// 调度管理
    /// </summary>
    public class DispatchController : UcController
    {
        public ActionResult Index(int pageIndex = 1)
        {
            int count = 0;//用于存放满足条件的记录总数
            IList<ICriterion> listQuery = GetCondition();//获取查询条件
            IList<Order> listOrder = new List<Order>() { new Order("ID", true) };//设置一个排序集合，new Order("ID", true)表示：根据ID排序,true代表升序,false代表降序
            IList<TransportOrder> list = Container.Instance.Resolve<ITransportOrderService>().GetPaged(listQuery, listOrder, pageIndex, PagerHelper.PageSize, out count);

            PageList<TransportOrder> pageList = list.ToPageList<TransportOrder>(pageIndex, PagerHelper.PageSize, count);//转换为PageList集合，用于分页控件显示不同的页码
            return View(pageList);//用pageList集合填充页面。
        }

        private IList<ICriterion> GetCondition()//生成查询条件,userName变量默认为空
        {
            List<ICriterion> queryConditions = new List<ICriterion>();//实例化一个查询条件集合
            queryConditions.Add(Expression.Not(Expression.Eq("Status", (int)TransportOrderStatus.Done))); //排除已经完成的        
            //queryConditions.Add(Expression.Eq("Creator.ID", AppHelper.LoginedUser.ID));//按照当前登录人员的ID查询（只能查询到自己创建的货物运单）
            return queryConditions;//返回查询条件集合
        }

        public ActionResult Create(int id)
        {
            TransportDataList();

            DeliveryForm form = new DeliveryForm();
            form.DeliveryDate = DateTime.Now;
            TransportOrder order = Container.Instance.Resolve<ITransportOrderService>().Get(id);
            form.TransportOrder = order;
            return View(form);
        }

        [HttpPost]
        public ActionResult Create(DeliveryForm form)
        {
                form.Creator = AppHelper.LoginedUser;
                Container.Instance.Resolve<IDeliveryFormService>().Create(form);
                return RedirectToAction("Index");
            //return Create(form.TransportOrder.ID);
        }

        private void TransportDataList()
        {
            TransportProvinceList();
            TransportVehicleList();
            TransportDriverList();
        }

        public ActionResult Edit(int id)
        {
            TransportDataList();
            DeliveryForm form = new DeliveryForm();

            IList<ICriterion> cris = new List<ICriterion>();
            cris.Add(Expression.Eq("TransportOrder.ID", id));
            IList<DeliveryForm> forms = Container.Instance.Resolve<IDeliveryFormService>().Find(cris);
            if (forms.Count == 1)
            {
                form = forms[0];
                ViewBag.ID = form.ID;
            }
            else
            {
                throw new Exception("id为{" + id.ToString() + "}所对应的送货单不存在！");
            }
            return View(form);
        }

        [HttpPost]
        public ActionResult Edit(DeliveryForm form)
        {
            Container.Instance.Resolve<IDeliveryFormService>().Update(form);
            return RedirectToAction("Index");
        }


        private void TransportVehicleList()
        {
            ViewBag.VehicleList = Container.Instance.Resolve<IVehicleService>().GetAll().OrderBy(o=>o.VehicleNumber).ToList();
        }

        private void TransportDriverList()
        {
            ViewBag.DriverList = Container.Instance.Resolve<IDriverService>().GetAll().OrderBy(o=>o.UserName).ToList();
        }

        private void TransportProvinceList()
        {
            ViewBag.ProvinceList = Container.Instance.Resolve<IProvinceService>().GetAll();
        }
    }
}
